package me.alide.modules.wechart.cp.controller;


import me.alide.modules.wechart.cp.config.WxCpConfiguration;
import me.alide.modules.wechart.cp.config.WxCpProperties;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpAgentWorkBench;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.bean.workbench.WorkBenchKeyData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wutairui
 * Created on 2020/4/5.
 */

@RestController
@RequestMapping("/wx/msg")
public class MsgController {


    @Autowired
    WxCpProperties wxCpProperties;

    @RequestMapping(value = "send")
    public String sendMsg(String msg) throws WxErrorException {
        WxCpMessage message = null;
        message = WxCpMessage.TEXT()
                .agentId(wxCpProperties.getAppConfigs().get(0).getAgentId())
                .toUser("WuTaiRui")
                .content(msg)
                .build();
        WxCpService wxCpService = WxCpConfiguration.getCpService(1000013);
        wxCpService.getMessageService().send(message);
        return "OK";
    }

    @RequestMapping(value = "webBench")
    public String webBench() throws WxErrorException {
        WxCpService wxCpService = WxCpConfiguration.getCpService(1000013);
        List<WorkBenchKeyData> workBenchKeyDataList = new ArrayList<>();
        WorkBenchKeyData workBenchKeyData = new WorkBenchKeyData();
        workBenchKeyData.setKey("未读");
        workBenchKeyData.setData("5");
        workBenchKeyData.setJumpUrl("http://qq.com");
        workBenchKeyData.setPagePath("pages/index");
        workBenchKeyDataList.add(workBenchKeyData);

        WorkBenchKeyData workBenchKeyData1 = new WorkBenchKeyData();
        workBenchKeyData1.setKey("待办");
        workBenchKeyData1.setData("8");
        workBenchKeyData1.setJumpUrl("http://qq.com");
        workBenchKeyData1.setPagePath("pages/index");
        workBenchKeyDataList.add(workBenchKeyData1);

        WorkBenchKeyData workBenchKeyData2 = new WorkBenchKeyData();
        workBenchKeyData2.setKey("已办");
        workBenchKeyData2.setData("12");
        workBenchKeyData2.setJumpUrl("http://qq.com");
        workBenchKeyData2.setPagePath("pages/index");
        workBenchKeyDataList.add(workBenchKeyData2);
        WorkBenchKeyData workBenchKeyData3 = new WorkBenchKeyData();
        workBenchKeyData3.setKey("转承办");
        workBenchKeyData3.setData("15");
        workBenchKeyData3.setJumpUrl("http://qq.com");
        workBenchKeyData3.setPagePath("pages/index");
        workBenchKeyDataList.add(workBenchKeyData3);

        WxCpAgentWorkBench wxCpAgentWorkBench = WxCpAgentWorkBench.builder().agentId(1000013L).type("keydata").keyDataList(workBenchKeyDataList).replaceUserData(true).build();
        wxCpService.getWorkBenchService().setWorkBenchTemplate(wxCpAgentWorkBench);
        return "OK";
    }
}
